/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.kj.api.vo.response.MemberProjectResponseVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.kj.entity.KjContributeProject;
import com.jeesite.modules.kj.dao.KjContributeProjectDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * kj_contribute_projectService
 * @author hzy
 * @version 2021-07-13
 */
@Service
@Transactional(readOnly=true)
public class KjContributeProjectService extends CrudService<KjContributeProjectDao, KjContributeProject> {

	/**
	 * 获取单条数据
	 * @param kjContributeProject
	 * @return
	 */
	@Override
	public KjContributeProject get(KjContributeProject kjContributeProject) {
		return super.get(kjContributeProject);
	}

	/**
	 * 查询分页数据
	 * @param kjContributeProject 查询条件
	 * @param kjContributeProject.page 分页对象
	 * @return
	 */
	@Override
	public Page<KjContributeProject> findPage(KjContributeProject kjContributeProject) {
		return super.findPage(kjContributeProject);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param kjContributeProject
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(KjContributeProject kjContributeProject) {
		super.save(kjContributeProject);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(kjContributeProject.getId(), "kjContributeProject_image");
	}

	/**
	 * 更新状态
	 * @param kjContributeProject
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(KjContributeProject kjContributeProject) {
		super.updateStatus(kjContributeProject);
	}

	/**
	 * 删除数据
	 * @param kjContributeProject
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(KjContributeProject kjContributeProject) {
		super.delete(kjContributeProject);
	}

	@Transactional(readOnly=false)
	public void updateContributeMember(String id){
		dao.updateContributeMember(id);
	}


	public List<MemberProjectResponseVo> memberContributeProjects(String memberId, int year, int month){
		KjContributeProject where = new KjContributeProject();
		where.setMemberId(memberId);
		if(year != 0 && month != 0){
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month - 1, 1);
			where.setCreateDateStr(DateUtils.formatDate(calendar.getTime()));
		}
		return dao.memberContributeProjects(where);
	}

	public BigDecimal getLastContributeIntegrals(KjContributeProject kjContributeProject){
		BigDecimal totalIntegrals = kjContributeProject.getTarget();
		BigDecimal contribute = kjContributeProject.getContribute();
		return totalIntegrals.subtract(contribute);
	}

}
